/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.Esporte_cont;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Esporte;

/**
 *
 * @author Diego
 */
public class JInternalFrame_esporte_cont extends javax.swing.JInternalFrame {

    /**
     * Creates new form JInternalFrame_esporte
     */
    public JInternalFrame_esporte_cont() {
        initComponents();
    }

    public void listar() {
        modelo = (DefaultTableModel) Tabela_esporte.getModel();
        modelo.setNumRows(0);
        esporte_cont = new Esporte_cont();
        for (int i = 0; i < esporte_cont.read().size(); i++) {
            modelo.addRow(new Object[]{esporte_cont.read().get(i).getIdesporte(),
                        esporte_cont.read().get(i).getNome()
                    });
            if (esporte_cont.read().get(i).getTipoesporte() == 0) {
                Tabela_esporte.setValueAt(false, i, 2);
            } else {
                Tabela_esporte.setValueAt(true, i, 2);
            }
        }
    }
    private DefaultTableModel modelo;
    private Esporte esporte;
    private Esporte_cont esporte_cont;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Tabela_esporte = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txt_busca_esporte = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btn_excluir = new javax.swing.JButton();
        btn_novo = new javax.swing.JButton();
        btn_sair = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Controle de esporte");

        Tabela_esporte.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nome", "Tipo de esporte"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        Tabela_esporte.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Tabela_esporteMouseClicked(evt);
            }
        });
        Tabela_esporte.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                Tabela_esporteAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        Tabela_esporte.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                Tabela_esporteKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(Tabela_esporte);

        jLabel1.setText("Digite o nome do esporte para busca-lo");

        txt_busca_esporte.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_busca_esporteKeyReleased(evt);
            }
        });

        jLabel2.setText("Tipo de esporte");

        btn_excluir.setText("Excluir");
        btn_excluir.setEnabled(false);
        btn_excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_excluirActionPerformed(evt);
            }
        });

        btn_novo.setText("Novo");
        btn_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_novoActionPerformed(evt);
            }
        });

        btn_sair.setText("Sair");
        btn_sair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_sairActionPerformed(evt);
            }
        });

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Esporte Olímpico");
        jCheckBox1.setEnabled(false);

        jCheckBox2.setText("Esporte Paralímpico");
        jCheckBox2.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_sair)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_novo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_excluir))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 789, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txt_busca_esporte))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCheckBox2)))
                        .addGap(19, 19, 19)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_busca_esporte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_excluir)
                    .addComponent(btn_novo)
                    .addComponent(btn_sair))
                .addContainerGap())
        );

        setBounds(0, 0, 825, 492);
    }// </editor-fold>//GEN-END:initComponents

    private void btn_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_novoActionPerformed
        modelo = (DefaultTableModel) Tabela_esporte.getModel();
        modelo.addRow(new Object[WIDTH]);
    }//GEN-LAST:event_btn_novoActionPerformed

    private void btn_sairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_sairActionPerformed
        dispose();
    }//GEN-LAST:event_btn_sairActionPerformed

    private void btn_excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_excluirActionPerformed
        esporte = new Esporte();
        esporte_cont = new Esporte_cont();
        modelo = (DefaultTableModel) Tabela_esporte.getModel();
        if (Tabela_esporte.getValueAt(Tabela_esporte.getSelectedRow(), 0) != null) {
            esporte.setIdesporte(Integer.valueOf(Tabela_esporte.getValueAt(Tabela_esporte.getSelectedRow(), 0).toString()));
            esporte_cont.delete(esporte);
            listar();
            btn_excluir.setEnabled(false);
            txt_busca_esporte.setText(null);
        } else {
            modelo.removeRow(Tabela_esporte.getSelectedRow());
        }
    }//GEN-LAST:event_btn_excluirActionPerformed

    private void Tabela_esporteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Tabela_esporteMouseClicked
        btn_excluir.setEnabled(true);
    }//GEN-LAST:event_Tabela_esporteMouseClicked

    private void txt_busca_esporteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_busca_esporteKeyReleased
        esporte = new Esporte();
        esporte_cont = new Esporte_cont();
        esporte.setNome(txt_busca_esporte.getText());
        modelo = (DefaultTableModel) Tabela_esporte.getModel();
        modelo.setNumRows(0);
        if (txt_busca_esporte.getText().equals("")) {
            listar();
            btn_excluir.setEnabled(false);
        } else {
            for (int i = 0; i < esporte_cont.search(esporte).size(); i++) {
                modelo.addRow(new Object[]{esporte_cont.search(esporte).get(i).getIdesporte(),
                            esporte_cont.search(esporte).get(i).getNome()
                        });
                if (esporte_cont.search(esporte).get(i).getTipoesporte() == 0) {
                    Tabela_esporte.setValueAt(false, i, 2);
                } else {
                    Tabela_esporte.setValueAt(true, i, 2);
                }

            }
            btn_excluir.setEnabled(false);
        }
    }//GEN-LAST:event_txt_busca_esporteKeyReleased

    private void Tabela_esporteAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_Tabela_esporteAncestorAdded
        listar();
    }//GEN-LAST:event_Tabela_esporteAncestorAdded

    private void Tabela_esporteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Tabela_esporteKeyPressed
        esporte = new Esporte();
        esporte_cont = new Esporte_cont();
        modelo = (DefaultTableModel) Tabela_esporte.getModel();
        if (Tabela_esporte.getValueAt(Tabela_esporte.getSelectedRow(), 0) == null) {
            esporte.setNome(Tabela_esporte.getValueAt(Tabela_esporte.getSelectedRow(), 1).toString());
            if (Tabela_esporte.getValueAt(Tabela_esporte.getSelectedRow(), 2) == true) {
                esporte.setTipoesporte(1);
            } else {
                esporte.setTipoesporte(0);
            }
            try {
                esporte_cont.create(esporte);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
            listar();
        } else {
            esporte.setIdesporte(Integer.valueOf(Tabela_esporte.getValueAt(Tabela_esporte.getSelectedRow(), 0).toString()));
            esporte.setNome(Tabela_esporte.getValueAt(Tabela_esporte.getSelectedRow(), 1).toString());
            if (Tabela_esporte.getValueAt(Tabela_esporte.getSelectedRow(), 2) == true) {
                esporte.setTipoesporte(1);
            } else {
                esporte.setTipoesporte(0);
            }
            try {
                esporte_cont.update(esporte);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
              
            }
            listar();
        }
    }//GEN-LAST:event_Tabela_esporteKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tabela_esporte;
    private javax.swing.JButton btn_excluir;
    private javax.swing.JButton btn_novo;
    private javax.swing.JButton btn_sair;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txt_busca_esporte;
    // End of variables declaration//GEN-END:variables
}
